package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.util.StrUtil;
import com.hornsun.domain.dbo.Material;
import com.hornsun.domain.dbo.MaterialExample;
import com.hornsun.repository.mapper.MaterialMapper;
import com.hornsun.util.LittleUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/5/7.
 */
@Repository
public class MaterialRepository {

    @Resource
    private MaterialMapper materialMapper;

    @Resource
    private LittleUtil littleUtil;

    /**
     * 获取最近两个素材
     * @return
     */
    public List<Material>  getLately(){
        MaterialExample materialExample = new MaterialExample();
        materialExample.setOrderByClause(StrUtil.toDesc("updateTime"));
        return materialMapper.selectByExampleWithRowbounds(materialExample,new RowBounds(0,2));
    };


    /**
     * 分类、分页获取素材
     * @param page
     * @return
     */
    public List<Material> getMaterial(int type,int page){
        MaterialExample materialExample = new MaterialExample();
        materialExample.setOrderByClause(StrUtil.toDesc("updateTime"));
        MaterialExample.Criteria criteria = materialExample.createCriteria();
        criteria.andTypeEqualTo((byte)type);
        return materialMapper.selectByExampleWithRowbounds(materialExample,littleUtil.bigRowBounds(page));
    }

    public long getCountByType(int type){
        MaterialExample materialExample = new MaterialExample();
        MaterialExample.Criteria criteria = materialExample.createCriteria();
        criteria.andTypeEqualTo((byte)type);
        return materialMapper.countByExample(materialExample);
    }


    /**
     * 新增素材
     * @param material
     */
    public void addMaterial(Material material){
        Preconditions.checkNotNull(material.getMaterialGuid());
        int count = materialMapper.insertSelective(material);
        if (count!=1){
            throw new IllegalArgumentException("add material failed by material:"+material);
        }
    }

    /**
     * 修改素材
     * @param material
     */
    public void updateMaterial(Material material){
        Preconditions.checkNotNull(material.getMaterialGuid());
        int count = materialMapper.updateByPrimaryKey(material);
        if (count!=1){
            throw new IllegalArgumentException("update material failed by material:"+material);
        }
    }


    /**
     * 删除素材
     * @param materialGuid
     */
    public void deleteMaterial(String materialGuid){
        Preconditions.checkNotNull(materialGuid);
        int count = materialMapper.deleteByPrimaryKey(materialGuid);
        if (count!=1){
            throw new IllegalArgumentException("delete material failed by materialGuid:"+materialGuid);
        }
    }


    public Material getMaterialByGuid(String materialGuid){
        Preconditions.checkNotNull(materialGuid);
        return materialMapper.selectByPrimaryKey(materialGuid);
    }



}
